package leetcode.lt100;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class Solution_95 {

    public static void main(String[] args) {
        new Solution_95().generateTrees(3);
    }

    public List<TreeNode> generateTrees(int n) {
        if (n == 0) {
            return new ArrayList<>();
        }
        return generate_trees(1, n);
    }

    private List<TreeNode> generate_trees(int start, int end) {
        List<TreeNode> treeNodes = new LinkedList<>();
        if (start > end) {
            treeNodes.add(null);
            return treeNodes;
        }
        for (int i = start; i <= end; i++) {
            List<TreeNode> lefts = generate_trees(start, i - 1);
            List<TreeNode> rights = generate_trees(i + 1, end);
            for (TreeNode left : lefts) {
                for (TreeNode right : rights) {
                    TreeNode treeNode = new TreeNode(i);
                    treeNode.left = left;
                    treeNode.right = right;
                    treeNodes.add(treeNode);
                }
            }
        }
        return treeNodes;
    }

}